Ext.define('Admin.view.authentication.Login', {
    extend: 'Admin.view.authentication.LockingWindow',
    xtype: 'login',

    requires: [
        'Admin.view.authentication.Dialog',
        'Ext.container.Container',
        'Ext.form.field.Text',
        'Ext.form.field.Checkbox',
        'Ext.button.Button'
    ],

    title: '',
    defaultFocus: 'authdialog',

    items: [{
        xtype: 'authdialog',
        defaultButton: 'loginButton',
        autoComplete: true,
        bodyPadding: '20 20',
        cls: 'auth-dialog-login',
        header: false,
        width: 415,
        layout: {
            type: 'vbox',
            align: 'stretch'
        },

        defaults: {
            margin: '5 0'
        },

        items: [{
                xtype: 'label',
                text: '输入用户名和密码'
            },
            {
                xtype: 'textfield',
                cls: 'auth-textbox',
                name: 'username',
                bind: '{username}',
                height: 55,
                hideLabel: true,
                allowBlank: false,
                blankText: '请填写用户名',
                emptyText: '用户名',
                maxLength: 32,
                triggers: {
                    glyphed: {
                        cls: 'trigger-glyph-noop auth-email-trigger'
                    }
                }
            },
            {
                xtype: 'textfield',
                cls: 'auth-textbox',
                height: 55,
                hideLabel: true,
                emptyText: '密码',
                inputType: 'password',
                name: 'password',
                bind: '{password}',
                allowBlank: false,
                blankText: '请填写密码',
                triggers: {
                    glyphed: {
                        cls: 'trigger-glyph-noop auth-password-trigger'
                    }
                }
            },
            {
                xtype: 'container',
                layout: 'hbox',
                items: [{
                        xtype: 'checkboxfield',
                        name: 'rememberMe',
                        flex: 1,
                        cls: 'form-panel-font-color rememberMeCheckbox',
                        height: 30,
                        bind: '{rememberMe}',
                        boxLabel: '记住我'
                    },
                    {
                        xtype: 'box',
                        //html: '<a href="#passwordreset" class="link-forgot-password"> 忘记密码 ？</a>'
                    }
                ]
            },
            {
                xtype: 'button',
                reference: 'loginButton',
                scale: 'large',
                ui: 'soft-green',
                iconAlign: 'right',
                iconCls: 'x-fa fa-angle-right',
                text: '登录',
                formBind: true,
                listeners: {
                    click: 'onLoginButton'
                }
            },
            {
                xtype: 'box',
                html: '<div class="outer-div"><div class="seperator">OR</div></div>',
                margin: '10 0'
            },
            {
                xtype: 'button',
                scale: 'large',
                ui: 'gray',
                iconAlign: 'right',
                iconCls: 'x-fa fa-user-plus',
                text: '注册',
                listeners: {
                    click: 'onNewAccount'
                }
            }
        ]
    }],

    initComponent: function() {
        this.addCls('user-login-register-container');
        this.callParent(arguments);
        var me = this;

        Ext.Ajax.request({
            url: 'system/checkMe',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code == 200) {
                    window.location.href = '#dashboard';
                    window.location.reload();
                    me.redirectTo('dashboard', true);
                }
            }
        });
    }
});